/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nc.kyiv.yellow.sociallibrary.reporting.web;

import com.nc.kyiv.yellow.sociallibrary.beans.reports.ReportingBeanLocal;
import com.nc.kyiv.yellow.sociallibrary.beans.reports.ReportingBeanLocalHome;
import com.nc.kyiv.yellow.sociallibrary.reporting.data.AvgReadSpeedReportFilter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author NightmaresSeller
 */
public class ReportingServlet extends HttpServlet {
    
    private ReportingBeanLocal reportingSB = null;
   
    public void init(ServletConfig servletConfig) throws ServletException {
        super.init(servletConfig);
        reportingSB = lookupReportingBeanLocal();
    }


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ReportingServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            if (reportingSB != null) {
                out.println("Reporting Session Bean Succesfully Loaded<br>");
                URL link = reportingSB.getBookInfoReport();
                out.println("Report demo.xls successfully created");
            } else {
                out.println("Failed to Load Reporting Session Bean");
            }
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }
    
    

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private ReportingBeanLocal lookupReportingBeanLocal() {
        try {
            Context c = new InitialContext();
            ReportingBeanLocalHome rv = (ReportingBeanLocalHome) c.lookup("java:comp/env/ReportingBean");
            return rv.create();
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        } catch (CreateException ce) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ce);
            throw new RuntimeException(ce);
        }
    }
}
